<template>
  <div>
    <el-table style="width: 100%" border :data="records">
      <el-table-column
        type="index"
        label="序号"
        width="80px"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="skuName"
        label="名称"
        width="width"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="skuDesc"
        label="描述"
        width="width"
        align="center"
      ></el-table-column>
      <el-table-column prop="" label="默认图片" width="110px" align="center">
        <template slot-scope="{ row }">
          <img
            :src="row.skuDefaultImg"
            alt=""
            style="width: 80px; height: 80px"
          />
        </template>
      </el-table-column>
      <el-table-column
        prop="weight"
        label="重量"
        width="80px"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="price"
        label="价格"
        width="80px"
        align="center"
      ></el-table-column>
      <el-table-column prop="" label="操作" width="width" align="center">
        <template slot-scope="{ row }">
          <el-button
            type="success"
            icon="el-icon-sort-up"
            size="mini"
            v-if="row.isSale == 0"
            @click="Sale(row)"
          ></el-button>
          <el-button
            type="warning"
            icon="el-icon-sort-down"
            size="mini"
            @click="cancel(row)"
            v-else
          ></el-button>
          <el-button
            type="primary"
            icon="el-icon-edit"
            size="mini"
            @click="edit"
          ></el-button>
          <el-button
            type="info"
            icon="el-icon-info"
            size="mini"
            @click="getSkuInfo(row)"
          ></el-button>
          <el-button
            type="danger"
            icon="el-icon-delete"
            size="mini"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- @size-change="handleSizeChange"
      @current-change="handleCurrentChange" 
      :current-page.sync="currentPage1"-->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="getSkuList"
      :current-page="page"
      :page-sizes="[3, 5, 10]"
      :page-size="limit"
      layout="prev, pager, next, jumper, ->,sizes, total"
      :total="total"
      style="text-align: center"
    >
    </el-pagination>
    <el-drawer :visible.sync="drawer" :show-close="false" size="50%">
      <el-row>
        <el-col :span="5">名称</el-col>
        <el-col :span="16">{{ skuInfo.skuName }}</el-col>
      </el-row>
      <el-row>
        <el-col :span="5">描述</el-col>
        <el-col :span="16">{{ skuInfo.skuDesc }}</el-col>
      </el-row>
      <el-row>
        <el-col :span="5">价格</el-col>
        <el-col :span="16">{{ skuInfo.price }}</el-col>
      </el-row>
      <el-row>
        <el-col :span="5">平台属性</el-col>
        <el-col :span="16">
          <template>
            <el-tag
              type="success"
              v-for="attr in skuInfo.skuAttrValueList"
              :key="attr.id"
              style="margin-right: 10px"
              >{{ attr.attrId }}--{{ attr.valueId }}</el-tag
            >
          </template>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="5">商品图片</el-col>
        <el-col :span="16">
          <template>
            <el-carousel height="500px">
              <el-carousel-item
                v-for="img in skuInfo.skuImageList"
                :key="img.id"
              >
                <img
                  :src="img.imgUrl"
                  alt=""
                  style="width: 100%; height: 100%"
                  class="small"
                />
              </el-carousel-item>
            </el-carousel>
          </template>
        </el-col>
      </el-row>
    </el-drawer>
  </div>
</template>

<script>
export default {
  name: "MySku",
  data() {
    return {
      // 当前页|初始页
      page: 1,
      // 每页展示几条数据
      limit: 10,
      // 总数据条数
      total: 0,
      // 服务器返回的数据列表(页码|每页展示多少数据由用户决定)
      records: [],
      //是否显示抽屉
      drawer: false,
      // sku详情
      skuInfo: {},
    };
  },
  mounted() {
    this.getSkuList();
  },
  methods: {
    async getSkuList(pages = 1) {
      //@current-change事件会传入当前页（如用户未主动选择页码则使用当前页的默认参数1） 然后向服务器请求当用户点击的当前页码列表数据
      this.page = pages;
      let result = await this.$API.sku.reqSkuList(this.page, this.limit);

      if (result.code === 200) {
        // 分页数据总条数
        this.total = result.data.total;
        // 当前页展示的数据列表
        this.records = result.data.records;
      }
    },
    // 当每页展示数据条数发生变化时
    handleSizeChange(limits) {
      this.limit = limits;
      this.getSkuList(this.page);
    },
    // sku上架
    async Sale(row) {
      let result = await this.$API.sku.reqSale(row.id);
      if (result.code === 200) {
        row.isSale = 1;
        this.$message({ type: "success", message: "上架成功" });
      }
    },
    // sku下架
    async cancel(row) {
      let result = await this.$API.sku.reqCancel(row.id);
      if (result.code === 200) {
        row.isSale = 0;
        this.$message({ type: "success", message: "下架成功" });
      }
    },
    edit() {
      this.$message({ type: "info", message: "正在开发" });
    },
    //  获取sku详情
    async getSkuInfo(row) {
      let result = await this.$API.sku.reqSkuById(row.id);
      if (result.code === 200) {
        this.skuInfo = result.data;
        this.drawer = true;
      }
    },
  },
};
</script>

<style>
.el-carousel__item h3 {
  color: #475669;
  font-size: 14px;
  opacity: 0.75;
  line-height: 150px;
  margin: 0;
}

.el-carousel__item:nth-child(2n) {
  background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n + 1) {
  background-color: #d3dce6;
}
</style>
<style scoped>
.el-row .el-col-5 {
  font-weight: 700;
  font-size: 18px;
  text-align: right;
}
.el-col {
  margin: 10px 10px;
}
/* 深度选择器【可以进行样式穿透】
  >>>  【原生cs】
  ::v-deep 【scss】
  /deep/ 【less】
  作用：对于父子组件 样式各自独立的情况下【scoped】父书写的元素样式只会影响子的根元素节点【子组件装内容的根容器】不会在往下影响
  如需要在上述情况下去影响子组件的其他深层元素节点 可以使用深度选择器进行样式穿透
 */
>>> .el-carousel__button {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: blue;
}
</style>